package com.example.util;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.graphics.drawable.PictureDrawable;
import android.view.View;
import android.webkit.WebView;

public class ImageUtil {
	public static byte[] convertBitmapToByteArray(Bitmap bitmap) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
		return stream.toByteArray();
	}

	public static Bitmap convertByteArrayToBitmap(byte[] byteArray) {
		if (byteArray != null)
			return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
		else
			return null;
	}
	
	public static Bitmap getBitmapFromView(View view) {
		view.buildDrawingCache();
		Bitmap bitmap = view.getDrawingCache();
		Bitmap newBitmap = bitmap.copy(bitmap.getConfig(), true);
		view.destroyDrawingCache();
		
		return newBitmap;
	}
	
	public static boolean captureScreen(View view, String file) {
		view.buildDrawingCache();
		Bitmap bitmap = view.getDrawingCache();
		FileUtil.makeDir(file);
	    FileUtil.delete(file);
	    boolean result = FileUtil.saveBitmap(bitmap, file);
	    view.destroyDrawingCache();
	    
		return result;
	}
	
	public static boolean captureScreen(View view, String file, int size) {
		view.buildDrawingCache();
		Bitmap bitmap = view.getDrawingCache();

		// resize bitmap
        bitmap = resizeBitmap(bitmap, size, size);
		
		FileUtil.makeDir(file);
	    FileUtil.delete(file);
	    boolean result = FileUtil.saveBitmap(bitmap, file);
	    view.destroyDrawingCache();
	    
		return result;
	}
	
	public static boolean captureWholeScreen(WebView view, String file) {
		Picture picture = view.capturePicture();
		PictureDrawable originalDrawable = new PictureDrawable(picture);
		Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPicture(originalDrawable.getPicture());
        
        return FileUtil.saveBitmap(bitmap, file);
	}
	
	public static boolean captureWholeScreen(WebView view, String file, int size) {
		// draw picture and make bitmap
		Picture picture = view.capturePicture();
		PictureDrawable originalDrawable = new PictureDrawable(picture);
		Bitmap bitmap = Bitmap.createBitmap(originalDrawable.getIntrinsicWidth(),originalDrawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPicture(originalDrawable.getPicture());
        
        // resize bitmap
        bitmap = resizeBitmap(bitmap, size, size);
	    
        return FileUtil.saveBitmap(bitmap, file);
    }
	
	public static Bitmap resizeBitmap(Bitmap bitmap, int width ,int height) {
		int fixedWidth = width, fixedHeight = height;
		if (bitmap.getHeight() > bitmap.getWidth())
			fixedHeight = fixedWidth * bitmap.getHeight() / bitmap.getWidth();
		else
			fixedWidth = fixedHeight * bitmap.getWidth() / bitmap.getHeight();
		
		Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, fixedWidth, fixedHeight, false);
		return Bitmap.createBitmap(newBitmap, 0, 0, width, height);
	}

	public static Bitmap getBitmapFromFile(String file) {
		if (!FileUtil.exists(file))
			return null;
		
		return BitmapFactory.decodeFile(file);
	}
}
